program principal
interface

function funcion(valor)result(res1)
	real(Kind=8)::valor,res1
end function

function derivada(valor)result(res2)
	real(Kind=8)::valor,res2
end function

subroutine calcular(x,it,tol)
	real(kind=8)::x,ant,tol,resfun,resder,error
	integer::maxint,i
end subroutine

subroutine datos(x,maxit,tol)
	real(kind=8)::x,tol
	integer::maxint
end subroutine

end interface



	real(kind=8)::x,tol
	integer::maxit
	call datos(x,maxit,tol)
	call calcular(x,maxit,tol)


end program

function funcion(x)result(res1)
	real(Kind=8),intent(in)::x
	real(kind=8)::res1
	!res1=(-x)**3+12*(x**2)-46*x+56
	res1=x**2._8-x - 1._8
end function

function derivada(x)result(res2)
	real(Kind=8),intent(in)::x
	real(kind=8)::res2
	!res2=(-3)*(x**2)+24*x-46
	res2=2._8*x-1._8
end function

subroutine calcular(x,it,tol)
	real(kind=8)::x,ant,tol,error
	integer::maxint,i
	interface
		function funcion(x)result(res1)
			real(Kind=8),intent(in)::x
			real(kind=8)::res1
		end function
		function derivada(x)result(res2)
			real(Kind=8),intent(in)::x
			real(kind=8)::res2
		end function
	end interface

	open(unit=1, file='newton.dat',status='unknown')
	write(1,*)'iteraciones: ',it,'tolerancia: ',tol
	write(1,*)'Aproximacion inicial'
	write(1,'(f17.14)')x
	do i=1,it,1
		ant=x
		x=x-funcion(x)/derivada(x)
		error=abs((x-ant)/x)
		
		write(1,*)'iteracion: ',i
		write(1,*)'Aproximacion'
		write(1,'(f17.14)'),x
		write(1,*)'error relativo: '
		write(1,'(e21.14)')error
		write(1,*)
		if(error<tol) then
			exit
		end if
	end do	
	close(1)
		
end subroutine



subroutine datos(x,maxit,tol)
	real(kind=8)::x,tol
	integer::maxit
	print*,'¿aproximación inicial?'
	read(5,*)x
	print*,'¿numero maximo de iteraciones?'
	read(5,*),maxit
	print*,'¿tolerancia de error?'
	read(5,*)tol
end subroutine
